package com.briup.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: xiongwenbin
 * @Date: 2024/7/23-07-23-上午11:33
 * @Description: org.example.briup.util
 */
public class backupUtil {
    public static void saveBackup(String path) throws IOException {
        InputStream in = backupUtil.class.getClassLoader().getResourceAsStream(path);
        System.out.println("in = " + in);
        // 流不阻塞的情况下 一次性读到的数据长度
        int length = in.available();
        in.close();
        File file = new File(path+"backup");
        if (!file.exists()) {
            file.createNewFile();
        }
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(file, true))) {
            bw.write(Integer.toString(length));
            bw.write("\n");
            System.out.println("备份完成。");
        } catch (IOException e) {
            System.err.println("写入文件时发生错误: " + e.getMessage());
        }
    }
    public static Long loadBackup(String path) throws IOException {
        File file = new File(path+"backup");
        if (!file.exists()) {
            System.out.println("备份文件不存在。");
            return 0L;
        }
        List<Long> list = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            list.add(Long.valueOf(line));
        }
        return list.get(list.size()-1);

    }
}
